/**
 * 
 */
package com.sc.softmgr.business.controller;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.acc.softmgr.model.SysUserMenu;
import com.acc.softmgr.model.SysUserMenuExample;
import com.alibaba.fastjson.JSONObject;
import com.sc.softmgr.business.service.ISysUserMenuService;
import com.sc.softmgr.common.constant.enums.RestStatus;

/**
 * 
 * @content
 * 
 * @date 2017年5月12日
 * 
 * @author OprCalf
 * 
 */
@Controller
@RequestMapping("sysUserMenu")
public class SysUserMenuController {

	@Autowired
	private ISysUserMenuService sysUserMenuService;

	@RequestMapping(value = "searchSelectUserByMenuIdAndUserId", method = RequestMethod.GET)
	@ResponseBody
	public JSONObject searchSelectUserByMenuIdAndUserId(@RequestParam Integer menuId, @RequestParam Integer userId)
			throws Exception {
		JSONObject json = new JSONObject();
		if (menuId == null || menuId == 0 || userId == null || userId == 0) {
			json.put("status", RestStatus.PARAMETER_REQUIRED.code);
			json.put("message", RestStatus.PARAMETER_REQUIRED.message);
		} else {
			SysUserMenuExample example = new SysUserMenuExample();
			example.getCriteria().andMenu_idEqualTo(menuId);
			example.getCriteria().andUser_idEqualTo(userId);
			List<SysUserMenu> sysUserMenus = sysUserMenuService.selectByExample(example);
			if (sysUserMenus != null && sysUserMenus.size() > 0) {
				json.put("status", RestStatus.SUCCESS.code);
				json.put("message", RestStatus.SUCCESS.message);
			}
		}
		return json;
	}

	/**
	 * 通过菜单关联批量用户
	 * 
	 * @param menuId
	 * @param userIds
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "link", method = RequestMethod.POST)
	@ResponseBody
	public JSONObject link(@RequestParam Integer menuId, @RequestParam String userIds) throws Exception {
		JSONObject json = new JSONObject();

		String[] users = userIds.split(",");
		if (StringUtils.isEmpty(userIds) || menuId == null) {
			json.put("status", RestStatus.PARAMETER_REQUIRED.code);
			json.put("message", RestStatus.PARAMETER_REQUIRED.message);
			return json;
		} else {
			SysUserMenuExample example = new SysUserMenuExample();
			example.getCriteria().andMenu_idEqualTo(menuId);
			List<SysUserMenu> list = sysUserMenuService.selectByExample(example);
			if (list != null && list.size() > 0) {
				for (SysUserMenu userMenu : list) {
					sysUserMenuService.deleteByPrimaryKey(userMenu.getId());// 删除已经关联用户
				}
			}
			for (String userId : users) {
				SysUserMenu psUserMenu = new SysUserMenu();
				psUserMenu.setMenu_id(menuId);
				psUserMenu.setUser_id(Integer.parseInt(userId));
				sysUserMenuService.insert(psUserMenu);
			}

			json.put("status", RestStatus.SUCCESS.code);
			json.put("message", RestStatus.SUCCESS.message);
			return json;
		}

	}

}
